package com.hqyj.paimaisys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hqyj.paimaisys.dao.AuctionDao;
import com.hqyj.paimaisys.dao.HuiyuanDao;
import com.hqyj.paimaisys.model.Auction;
import com.hqyj.paimaisys.model.Dealrecord;
import com.hqyj.paimaisys.dao.DealrecordDao;
import com.hqyj.paimaisys.model.Huiyuan;
import com.hqyj.paimaisys.service.DealrecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-10-20
 */
@Service
public class DealrecordServiceImpl extends ServiceImpl<DealrecordDao, Dealrecord> implements DealrecordService {

    @Autowired
    private HuiyuanDao huiyuanDao;

    @Autowired
    private AuctionDao auctionDao;


    /**
     * 付款
     * @param updateWrapper  ： 会员表的wrapper
     * @param updateWrapper2 :  DealRecordWrapper
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void fukuan(UpdateWrapper<Huiyuan> updateWrapper, UpdateWrapper<Dealrecord> updateWrapper2) {

        huiyuanDao.update(null, updateWrapper);
        baseMapper.update(null, updateWrapper2);
    }

    /**
     * 违约
     */
    @Override
    public void weiyue() {
        //select * from dealreacord  where createtime = ? and state = 4
        Calendar c = Calendar.getInstance(); //当前时间的日历类
        c.add(Calendar.DAY_OF_MONTH, -3);
        Date ago = c.getTime();
        String agoStr = new SimpleDateFormat("yyyy-MM-dd").format(ago);

        QueryWrapper<Dealrecord> queryWrapper = new QueryWrapper<Dealrecord>();
        queryWrapper.eq("createtime", agoStr);
        queryWrapper.eq("did", Dealrecord.STATUS_DAIFUKUAN);

        List<Dealrecord> list = baseMapper.selectList(queryWrapper);

        list.forEach(deal->{
            //让每一个deal对应的会员扣除保证金。
            Huiyuan hy = huiyuanDao.selectById(deal.getHid());
            Auction auction = auctionDao.selectById(deal.getAid());
            auction.getAbmoney();

            UpdateWrapper<Huiyuan> updateWrapper = new UpdateWrapper<Huiyuan>();

            updateWrapper.set("hicemoney", hy.getHicemoney()-auction.getAbmoney());
            updateWrapper.eq("hid", hy.getHid());
            huiyuanDao.update(null, updateWrapper);
        });

        // update dealreacord  set state = 3, cjtime = ? where createtime = ? and state = 4

        UpdateWrapper<Dealrecord> updateWrapper2 = new UpdateWrapper<Dealrecord>();
        updateWrapper2.set("did",Dealrecord.STATUS_NO);
        updateWrapper2.eq("createtime", agoStr);
        updateWrapper2.eq("state", Dealrecord.STATUS_DAIFUKUAN);

        baseMapper.update(null, updateWrapper2);


    }
}
